/*
 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. This
 * code is released under a tri EPL/GPL/LGPL license. You can use it,
 * redistribute it and/or modify it under the terms of the:
 *
 * Eclipse Public License version 1.0
 * GNU General Public License version 2
 * GNU Lesser General Public License version 2.1
 */
package org.truffleruby.platform;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ErrnoDescriptions {

    private static final Map<String, String> DESCRIPTIONS;

    public static String getDescription(String errnoName) {
        final String description = DESCRIPTIONS.get(errnoName);
        assert description != null;
        return description;
    }

    static {
        // From Linux error messages according to strerror()
        // Generated by tool/generate-errno-descriptions.rb
        final Map<String, String> map = new HashMap<>();
        map.put("EPERM", "Operation not permitted");
        map.put("ENOENT", "No such file or directory");
        map.put("ESRCH", "No such process");
        map.put("EINTR", "Interrupted system call");
        map.put("EIO", "Input/output error");
        map.put("ENXIO", "No such device or address");
        map.put("E2BIG", "Argument list too long");
        map.put("ENOEXEC", "Exec format error");
        map.put("EBADF", "Bad file descriptor");
        map.put("ECHILD", "No child processes");
        map.put("EDEADLK", "Resource deadlock avoided");
        map.put("ENOMEM", "Cannot allocate memory");
        map.put("EACCES", "Permission denied");
        map.put("EFAULT", "Bad address");
        map.put("ENOTBLK", "Block device required");
        map.put("EBUSY", "Device or resource busy");
        map.put("EEXIST", "File exists");
        map.put("EXDEV", "Invalid cross-device link");
        map.put("ENODEV", "No such device");
        map.put("ENOTDIR", "Not a directory");
        map.put("EISDIR", "Is a directory");
        map.put("EINVAL", "Invalid argument");
        map.put("ENFILE", "Too many open files in system");
        map.put("EMFILE", "Too many open files");
        map.put("ENOTTY", "Inappropriate ioctl for device");
        map.put("ETXTBSY", "Text file busy");
        map.put("EFBIG", "File too large");
        map.put("ENOSPC", "No space left on device");
        map.put("ESPIPE", "Illegal seek");
        map.put("EROFS", "Read-only file system");
        map.put("EMLINK", "Too many links");
        map.put("EPIPE", "Broken pipe");
        map.put("EDOM", "Numerical argument out of domain");
        map.put("ERANGE", "Numerical result out of range");
        map.put("EWOULDBLOCK", "Resource temporarily unavailable");
        map.put("EAGAIN", "Resource temporarily unavailable");
        map.put("EINPROGRESS", "Operation now in progress");
        map.put("EALREADY", "Operation already in progress");
        map.put("ENOTSOCK", "Socket operation on non-socket");
        map.put("EDESTADDRREQ", "Destination address required");
        map.put("EMSGSIZE", "Message too long");
        map.put("EPROTOTYPE", "Protocol wrong type for socket");
        map.put("ENOPROTOOPT", "Protocol not available");
        map.put("EPROTONOSUPPORT", "Protocol not supported");
        map.put("ESOCKTNOSUPPORT", "Socket type not supported");
        map.put("EOPNOTSUPP", "Operation not supported");
        map.put("EPFNOSUPPORT", "Protocol family not supported");
        map.put("EAFNOSUPPORT", "Address family not supported by protocol");
        map.put("EADDRINUSE", "Address already in use");
        map.put("EADDRNOTAVAIL", "Cannot assign requested address");
        map.put("ENETDOWN", "Network is down");
        map.put("ENETUNREACH", "Network is unreachable");
        map.put("ENETRESET", "Network dropped connection on reset");
        map.put("ECONNABORTED", "Software caused connection abort");
        map.put("ECONNRESET", "Connection reset by peer");
        map.put("ENOBUFS", "No buffer space available");
        map.put("EISCONN", "Transport endpoint is already connected");
        map.put("ENOTCONN", "Transport endpoint is not connected");
        map.put("ESHUTDOWN", "Cannot send after transport endpoint shutdown");
        map.put("ETOOMANYREFS", "Too many references: cannot splice");
        map.put("ETIMEDOUT", "Connection timed out");
        map.put("ECONNREFUSED", "Connection refused");
        map.put("ELOOP", "Too many levels of symbolic links");
        map.put("ENAMETOOLONG", "File name too long");
        map.put("EHOSTDOWN", "Host is down");
        map.put("EHOSTUNREACH", "No route to host");
        map.put("ENOTEMPTY", "Directory not empty");
        map.put("EUSERS", "Too many users");
        map.put("EDQUOT", "Disk quota exceeded");
        map.put("ESTALE", "Stale file handle");
        map.put("EREMOTE", "Object is remote");
        map.put("ENOLCK", "No locks available");
        map.put("ENOSYS", "Function not implemented");
        map.put("EOVERFLOW", "Value too large for defined data type");
        map.put("EIDRM", "Identifier removed");
        map.put("ENOMSG", "No message of desired type");
        map.put("EILSEQ", "Invalid or incomplete multibyte or wide character");
        map.put("EBADMSG", "Bad message");
        map.put("EMULTIHOP", "Multihop attempted");
        map.put("ENODATA", "No data available");
        map.put("ENOLINK", "Link has been severed");
        map.put("ENOSR", "Out of streams resources");
        map.put("ENOSTR", "Device not a stream");
        map.put("EPROTO", "Protocol error");
        map.put("ETIME", "Timer expired");

        DESCRIPTIONS = Collections.unmodifiableMap(map);
    }

}
